/*
 * Copyright (C) 2019 ~ 2021 Uniontech Software Technology Co.,Ltd.
 *
 * Author:     yanghongwei <yanghongwei@uniontech.com>
 *
 * Maintainer: yanghongwei <yanghongwei@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "http_server.h"
#include "http_process.h"

#define SSL_HTTP

#include <Poco/StreamCopier.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/Net/TCPServer.h>
#include <Poco/Net/TCPServerConnectionFactory.h>
#include <Poco/Net/DNS.h>
#include <Poco/Net/ServerSocket.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Net/SocketStream.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Net/SocketStream.h>
#include <Poco/Net/HTTPServerParams.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/Net/HTTPServerResponse.h>
#ifdef SSL_HTTP
#include <Poco/Net/ConsoleCertificateHandler.h>
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/Net/Context.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/Net/SecureServerSocket.h>
#include <Poco/Net/NetSSL.h>
#include <Poco/Net/HTTPSStreamFactory.h>
#endif

const std::string kServerCertPath = "./sources/server/server-cert.pem"; // 服务端证书文件
const std::string kPrivateKeyPath = "./sources/server/server-key.pem"; // 私钥文件
const std::string kRootCertPath = "./sources/ca/ca-cert.pem"; // CA根证书文件

using Poco::Net::DNS;
using Poco::Net::HostEntry;
using Poco::Net::IPAddress;

class MyRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
{
public:
    MyRequestHandlerFactory() {};
    ~MyRequestHandlerFactory() {};

public:
    virtual Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &request)
    {
        return new RootHandler;
    }
};

int MyServerApp::main(const std::vector<std::string> &)
{
    Poco::UInt16 port = 1080;
    Poco::Net::HTTPServerParams *pParams = new Poco::Net::HTTPServerParams;
    pParams->setMaxQueued(100);
    pParams->setMaxThreads(16);

#ifdef SSL_HTTP
    // initialize SSL
    Poco::Net::initializeSSL();

    // Poco::Net::HTTPSStreamFactory::registerFactory();

    // Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> pCert =
    //     new Poco::Net::ConsoleCertificateHandler(false);
    Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> pCert =
        new Poco::Net::AcceptCertificateHandler(false);
    Poco::Net::Context::Ptr pContext =
        new Poco::Net::Context(
            Poco::Net::Context::SERVER_USE,
            kPrivateKeyPath,
            kServerCertPath,
            kRootCertPath);
    Poco::Net::SSLManager::instance().initializeServer(0, pCert, pContext);
    // Poco::Net::SSLManager::instance().initializeClient(0, pCert, pContext);
#endif // SSL
    // if (port == Poco::Net::Context::UNKNOWN_PORT)
    //     return 1;
    // set-up a server socket
#ifdef SSL_HTTP
    Poco::Net::SecureServerSocket svs(port, 64, pContext);
#else
    Poco::Net::ServerSocket svs(port); // set-up a server socket
#endif // SSL

    Poco::Net::HTTPServer srv(new MyRequestHandlerFactory(), svs, pParams);
    // start the HTTPServer
    srv.start();
    waitForTerminationRequest();
    // Stop the HTTPServer
    srv.stop();

    return Application::EXIT_OK;
}